/**
 * Created by jiangzhiyong on 15/10/17.
 */

var Manager = {
    init: function() {
        $("#register_btn").click(Manager.register);
    },
    //登录，拿到token和sessionID
    register : function() {
    		//读取token，如果有就自动登录，如果没有就跳转到登录页面
    		var mobile = $("#mobile").val();
    		if (mobile == "") {
    			alert("请填写合法手机号");
    			return false;
    		}
    		var captcha = $("#captcha").val();
    		if (captcha == "") {
    			alert("请填写");
    			return false;
    		}
    		var password = $("#password").val();
    		if (password == "") {
    			alert("请填写密码");
    			return false;
    		}
    		var name = $("#name").val();
    		if (name == "") {
    			alert("请填写姓名");
    			return false;
    		}
    		var nick = $("#nick").val();
    		if (nick == "") {
    			alert("请填写昵称");
    			return false;
    		}
    		var gender = $("#gender").val();
    		if (gender == "") {
    			alert("请填写性别");
    			return false;
    		}
    		var avatar = $("#avatar").val();
    		var birthday = $("#birthday").val();
    		var slogan = $("#slogan").val();
    		var wiki = $("#wiki").val();
    		var city_code = $("#city_code").val();
    		var location = $("#location").val();
    		var coordinate = $("#coordinate").val();
    		
		var data = {
			"mobile": mobile,
			"user_type": 2,
			"captcha": captcha,
			"password": calcMD5(password + "meetclass" + password)
			"name": name,
			"nick": nick,
			"gender": gender,
			"avatar": avatar,
			"birthday": birthday,
			"slogan": slogan,
			"wiki": wiki,
			"city_code": city_code,
			"location": location,
			"coordinate": coordinate
		};
		var host = "http://t2.7kez.com";
	    $.ajax({
	        url: host + "/register",
	        type: 'post',
	        async: false,
	        data: data,
	        dataType: 'json',
	        crossDomain:true,
	        success: function (result) {
	            /**
	             * {
					    "code": 0,
					    "data": {
					        "sid": "2de1666ab60b48da86e5f36425a449a4",
					        "token": "7ea7cd7799830ce7955e15504cb2609b",
					        "user": {
					            "birthday": 0,
					            "created_at": 1.44454829e+09,
					            "gender": 1,
					            "id": 3,
					            "mobile": "13800000003",
					            "name": "a2",
					            "nick": "a2",
					            "status": 0,
					            "type": 1
					        }
					    }
					}
	             */
	            if (result.code == 0) {
	            		//写入token、写入session
	            		var token = result.data.token;
	            		var session_id = result.data.sid;
	            		var user_id = result.data.user.id;
	            		var name = result.data.user.name;
	            		$.cookie('token', token, { expires: Number.MAX_VALUE, path: '/' });
	            		$.cookie('session_id', session_id, { expires: Number.MAX_VALUE, path: '/' });
	            		$.cookie('user_id', user_id, { expires: Number.MAX_VALUE, path: '/' });
	            		$.cookie('name', name, { expires: Number.MAX_VALUE, path: '/' });
	            		//跳转到首页
	            		window.location.href = "index.html";
	            } else {
	            		alert("注册失败! 用户名或密码错误");
	            }
	        }
	    });
    }
};

$(function(){
    Manager.init();
})
